<?php
/*
	评论
*/
namespace hotel\modules\api\controllers;


use hotel\common\models\Comment;
use hotel\common\utils\Tool;



class CommentController extends CoreController {
	private $star=['环境','服务','设施','卫生','早餐','位置'];
	private $tags=['服务周到','环境舒适','早餐很棒','干净卫生','离恐龙园很近','孩子很喜欢','停车方便','出了酒店就是迪诺水镇'];
	//返回评分
	public function actionGetStar(){
		return Tool::showRes([0, '成功'], $this->star);
	}
	//返回标签
	public function actionGetTag(){
		return Tool::showRes([0, '成功'], $this->tags);
	}
	//列表
	public function actionList(){
		$get= \Yii::$app->request->get();
		$list = Comment::lists($get);
		return Tool::showRes([0, '成功'], $list);
	}
	//添加q
	public function actionAdd(){
		$post= \Yii::$app->request->post();
		if($post['order_id']){
			$check = Comment::find()->where(['order_id'=>$post['order_id']])->asArray()->one();
			if($check)
				return Tool::showRes([0, '已评论过']);
			$star_json = json_decode($post['star_json'],true);
			$star_all=0;
			foreach($star_json as $v){
				$star_all = $star_all+$v['star'];
			}
			$post['face']=base64_encode($post['face']);
			$post['star'] = (string)ceil($star_all/count($star_json));
			
			
			$data = Comment::setData($post);
			if(!$data){
			    return Tool::showRes([0, '成功']);
			}
		}
		
		return Tool::showRes([400, '失败'],$data);
	}
	//报表111
	public function actionAll(){
		$get= \Yii::$app->request->get();
		$avg = Comment::find()
			->select(['count(*) as num','star'])
			->where(['appid'=>$get['appid'],'is_active'=>1])
			->groupBy('star')
			->asArray()->all();
		$all = Comment::find()->where(['appid'=>$get['appid'],'is_active'=>1])->count();
		$avg_star = Comment::find()->select(['avg(star) as star'])->where(['appid'=>$get['appid'],'is_active'=>1])->asArray()->all();
		$star=['5'=>0,'4'=>0,'3'=>0,'2'=>0,'1'=>0];
		foreach ($avg as $v){
			$star[$v['star']] = (int)$v['num'];
		}
		$tags = $this->tags;
		$res = [];
		foreach($tags as $v){
			$num = Comment::find()
			->where(['appid'=>$get['appid'],'is_active'=>1])
			->andWhere(['like','tags',$v])
			->count();
			$res[]=[
				'tag'=>$v,
				'num'=>$num
			];
		}
		return Tool::showRes([0, '成功'], ['star'=>['all_star'=>$star,'all_num'=>$all,'avg_star'=>$avg_star[0]['star']?substr($avg_star[0]['star'],0,3):0],'tags'=>$res]);
	}
}